package edu.nf.llmtest.service.Handler;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.nf.llmtest.common.annotation.MessageType;
import edu.nf.llmtest.mapper.UserMapper;
import edu.nf.llmtest.model.dto.MessageEnvelope;
import edu.nf.llmtest.model.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * @ Author bin
 * @ Create  2025/10/28 上午9:42
 **/
@MessageType("delUser")
@Service
@Slf4j
@RequiredArgsConstructor
public class DeleteUserHanlder implements MessageHandler{
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final UserMapper userMapper;

    @Override
    public void handle(Message message) {
        String body = new String(message.getBody(), StandardCharsets.UTF_8);

        try {
            MessageEnvelope<User> wrapper = objectMapper.readValue(
                    body,
                    new TypeReference<MessageEnvelope<User>>() {}
            );
            User user = wrapper.getData();
            log.info("删除: {}", user.getEmail());
            userMapper.deleteUserByEmail(user.getEmail());
        } catch (Exception e) {
            log.error("解析用户消息失败: {}", body, e);
        }
    }
}
